import csv
import time
from selenium import webdriver
# 等待
from selenium.webdriver.support.wait import WebDriverWait
# 期望
from selenium.webdriver.support import expected_conditions as EC
# 调用键盘事件
from selenium.webdriver.common.keys import Keys
# 负责定位
from selenium.webdriver.common.by import By

f = open('拉勾招聘前端工作数据.csv', 'w', encoding='utf-8', newline='')
# 创建写操作
mywrite = csv.writer(f)
# 写列名
mywrite.writerow(['job', 'address', 'salary', 'experience', 'EB', 'company'])

# 创建谷歌浏览器设置对象
options = webdriver.ChromeOptions()
# 不加载图片, 提升速度
# options.add_argument('blink-settings=imagesEnabled=false')
# # 解决DevToolsActivePort文件不存在的报错
# options.add_argument('--no-sandbox')
# # 谷歌文档提到需要加上这个属性来规避bug
# options.add_argument('--disable-gpu')

# 创建浏览器对象
browser = webdriver.Chrome('chromedriver.exe', options=options)
# 使浏览器窗口最大化
browser.maximize_window()
URL = 'https://www.lagou.com/wn/jobs'
# 请求网址
browser.get(url=URL)

# 防止selenium被监测
# 先修改js，再加载js
browser.execute_cdp_cmd(
    "Page.addScriptToEvaluateOnNewDocument",
    {
        "source": "Object.defineProperty(navigator, 'webdriver', {get: () =>undefined})"
    }
)

time.sleep(3)
# goods = input('请输入职位、公司或地点:')
# 在搜索框输入商品
# send_keys()向浏览器页面定位的某个元素发送信息（向输入框输入内容、按键事件等等）
browser.find_element(By.CSS_SELECTOR, '#keyword').send_keys('前端', Keys.ENTER)
# 点击回车
# browser.find_element(By.CSS_SELECTOR, '#jobsContainer > div.search-wrap__3oGmR > div > div.input-wrap__3mngC > button').click()
for k in range(20):
    time.sleep(5)
    # WebDriverWait(browser, 5).until(
    #     EC.text_to_be_present_in_element_value((By.CSS_SELECTOR, '#order > div > div.option__21bte.active__YmPJe', '最新'))
    # )
    div_list = browser.find_elements(By.CSS_SELECTOR, '#jobList > div.list__YibNq > div')
    # print(div_list, len(div_list))

    for i in div_list:
        title = i.find_element(By.CSS_SELECTOR, 'div > div.item-top__1Z3Zo > div.position__21iOS > div.p-top__1F7CL > a').text
        # salary = i.find_element(By.CSS_SELECTOR, 'div.item-top__1Z3Zo > div.position__21iOS > div.p-bom__JlNur > span').text
        company = i.find_element(By.CSS_SELECTOR, 'div > div.item-top__1Z3Zo > div.company__2EsC8 > div.company-name__2-SjF > a').text
        experience = i.find_element(By.CSS_SELECTOR, 'div > div.item-top__1Z3Zo > div.position__21iOS > div.p-bom__JlNur').text
        job = title.split('[')[0]
        address = title.split('[')[1][0:-1]
        salary = experience.split('经验')[0]
        experience1 = experience.split('经验')[1]
        experience2 = experience1.split('/')[0]
        EB = experience1.split('/')[1]

        print(job, address, salary, experience2, EB, company)
        mywrite.writerow(['job', 'address', 'salary', 'experience', 'EB', 'company'])
    time.sleep(3)
    browser.find_element(By.CSS_SELECTOR, '#jobList > div.pagination__1L2PP > ul > li.lg-pagination-next').click()
